
let ajax = getApp().globalData.ajax;

Page({
  data: {
    navRouter: [
      { isSelect: true, title: '未完成', isFinish: false },
      { isSelect: false, title: '已完成', isFinish: true },
    ],
    isFinish: false,
    orderList: [],
    skip: 0,
    take: 5,
    hasNext: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let json = {
      token: wx.getStorageSync('Cookie'),
      isFinish: this.data.isFinish,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json);

  },

  chengName (e) {
    let index = e.target.dataset.index;
    let navRouter = this.data.navRouter;

    navRouter.forEach((item, ind) => {
      item.isSelect = ind === index ? true : false;
    })
    this.setData({
      navRouter,
      skip: 0,
      orderList: [],
      hasNext: true,
      isFinish: navRouter.find((item, ind) => ind === index).isFinish,
    });
    let json = {
      token: wx.getStorageSync('Cookie'),
      isFinish: this.data.isFinish,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json);

  },

  getData (json) {
    ajax('order/isFinish', json, ).then(res => {
      let orderList = res.data || [];

      if (!orderList.length) {
        this.setData({
          hasNext: false,
        })
      }

      orderList.forEach(item => {
        item.createTimeRes = item.create_time.toDateString();
        item.priceRes = (item.price / 100).toFixed(2);
        switch (item.status) {
          case 'INIT':
            item.statusRes = '未支付';
            break;
          case 'BUYER_START':
            item.statusRes = '未支付';
            break;
          case 'PAYED':
            item.statusRes = '已支付';
            break;
          case 'CANCELED':
            item.statusRes = '已取消';
            break;
          case 'READY':
            item.statusRes = '已确认';
            break;
          case 'ONGOING':
            item.statusRes = '进行中';
            break;
          case 'FINISH_AUTO':
            item.statusRes = '已完成';
            break;
          case 'FINISH':
            item.statusRes = '已完成';
            break;
          case 'PAYED':
            item.statusRes = '已支付';
            break;
          case 'PAYED_PAY':
            item.statusRes = '已支付';
            break;
          case 'SELLER_START':
            item.statusRes = '已接单';
            break;
          case 'DENY':
            item.statusRes = '已拒绝';
            break;
          case 'REFUND':
            item.statusRes = '已退款';
            break;
        }
      })

      orderList = this.data.orderList.concat(orderList);
      this.setData({
        orderList,
      })
    })
  },


  toDetail (e) {
    let orderId = e.currentTarget.dataset.orderid;

    wx.navigateTo({
      url: `../anchorsOrderDetail/anchorsOrderDetail?id=${orderId}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNext) {
      wx.showToast({
        title: '没有更多数据',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return
    }

    let skip = this.data.skip;
    skip += this.data.take;
    this.setData({
      skip,
    })

    let json = {
      token: wx.getStorageSync('Cookie'),
      isFinish: this.data.isFinish,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})